﻿///Nokia Firmware Cooker - tool for editing Nokia 5800 and 5530 firmwares.
///Copyright (C) 2010  Kalashnikov I.N.
///
///This program is free software; you can redistribute it and/or
///modify it under the terms of the GNU General Public License
///as published by the Free Software Foundation; either version 2
///of the License, or (at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace NokiaFirmwareCooker
{
    public partial class AddSoftDialog : Form
    {
        Form1 form;

        public AddSoftDialog(Form1 f)
        {
            InitializeComponent();
            this.form = f;
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            if (openSisDialog.ShowDialog() == DialogResult.OK)
            {
                programPathText.Text = openSisDialog.FileName;
                programPathText.Enabled = true;
                programNameText.Enabled = true;
                string tmp = programPathText.Text.Substring(programPathText.Text.LastIndexOf('\\') + 1);
                tmp = tmp.Substring(0, tmp.LastIndexOf('.'));
                programNameText.Text = tmp;
                addButton.Enabled = true;
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            string data;
            data = programNameText.Text;

            string dir = Application.StartupPath + "\\temp\\";
            string file = Application.StartupPath + "\\temp\\" + programNameText.Text + ".sis";
            string param = "-x -nopkg \"" + file + "\" -d \"" + dir + "\"";

            new FileInfo(programPathText.Text).CopyTo(file, true);
            ProcessStartInfo exe = new ProcessStartInfo(Application.StartupPath + "\\exe\\extsis.exe", param);
            exe.CreateNoWindow = true;
            exe.WindowStyle = ProcessWindowStyle.Hidden;
            using (Process p = Process.Start(exe))
            {
                p.WaitForExit();
            }

            form.AddSoft(data);
            this.Close();
        }
    }
}
